﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OrkutAPILibrary.Constants;
using OrkutAPILibrary.Entities;
using OrkutAPILibrary.Enums;

namespace OrkutAPILibrary.Methods
{
    /// <summary>
    /// Orkut Activity Factory
    /// </summary>
    static class OrkutActivityFactory
    {
        /// <summary>
        /// Gets the self activities.
        /// </summary>
        /// <param name="startIndex">The 1-based index of the first result to be retrieved (for paging).</param>
        /// <param name="count">The number of items per page, for a paged collection. The default value is 20.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutActivity> GetSelfActivities(int startIndex = 0, int count = OrkutLibraryConstants.DefaultNoOfActivities)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "activities.get"},
                                    {"userId", "@me"},
                                    {"groupId", "@all"},
                                    {"coalesce", "true"},
                                    {"count", count.ToString()},
                                    {"startIndex", startIndex.ToString()}
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse) ? null : GetActivities(requestResponse);
        }

        /// <summary>
        /// Gets the activities.
        /// </summary>
        /// <param name="requestResponse">The request response.</param>
        /// <returns></returns>
        private static OrkutData<OrkutActivity> GetActivities(string requestResponse)
        {
            var albums = JsonConvert.DeserializeObject<OrkutData<JObject>>(requestResponse);
            var newList = new List<OrkutActivity>();
            foreach (var obj in albums.List)
            {
                var actType = (ActivityType)Enum.Parse(typeof(ActivityType), obj["templateParams"]["activityType"].Value<string>());
                OrkutActivity res = null;
                switch (actType)
                {
                    case ActivityType.ALBUM:
                        break;
                    case ActivityType.ALBUM_SHARE:
                        break;
                    case ActivityType.APPLICATION:
                        break;
                    case ActivityType.COMMUNITY_CREATE:
                        break;
                    case ActivityType.COMMUNITY_JOIN:
                        break;
                    case ActivityType.FRIEND_ADD:
                        break;
                    case ActivityType.MAKAMAKA:
                        break;
                    case ActivityType.PHOTO:
                        res = JsonConvert.DeserializeObject<OrkutPhotoShareActivity>(obj.ToString());
                        break;
                    case ActivityType.PHOTO_COMMENT:
                        break;
                    case ActivityType.PHOTO_TAG:
                        break;
                    case ActivityType.PROFILE_UPDATE:
                        res = JsonConvert.DeserializeObject<OrkutProfileUpdateActivity>(obj.ToString());
                        break;
                    case ActivityType.SCRAP:
                        break;
                    case ActivityType.SKIN_CHANGE:
                        break;
                    case ActivityType.SOCIAL_EVENTS_CREATION:
                        break;
                    case ActivityType.STATUS_MSG:
                        break;
                    case ActivityType.STATUS_MSG_COMMENT:
                        break;
                    case ActivityType.TESTIMONIAL:
                        break;
                    case ActivityType.TESTIMONIAL_USER:
                        break;
                    case ActivityType.VIDEO:
                        res = JsonConvert.DeserializeObject<OrkutVideoShareActivity>(obj.ToString());
                        break;
                    case ActivityType.VIDEO_COMMENT:
                        break;
                    default:
                        break;
                }

                newList.Add(res);
            }
            return new OrkutData<OrkutActivity>
                       {
                           StartIndex = albums.StartIndex,
                           Filtered = albums.Filtered,
                           Sorted = albums.Sorted,
                           TotalResults = albums.TotalResults,
                           UpdatedSince = albums.UpdatedSince,
                           List = newList.ToArray(),
                       };
        }

        /// <summary>
        /// Gets the activities of a particular user.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="startIndex">The 1-based index of the first result to be retrieved (for paging).</param>
        /// <param name="count">The number of items per page, for a paged collection. The default value is 20.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutActivity> GetActivitiesOfUser(string userId, int startIndex = 0, int count = OrkutLibraryConstants.DefaultNoOfActivities)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "activities.get"},
                                    {"userId", userId},
                                    {"groupId", "@all"},
                                    {"coalesce", "true"},
                                    {"count",count.ToString()},
                                    {"startIndex",startIndex.ToString()},
                                    
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse) ? null : GetActivities(requestResponse);
        }

        /// <summary>
        /// Posts the activity.
        /// </summary>
        /// <param name="activityTitle">The activity title.</param>
        /// <param name="activityBody">The activity body.</param>
        /// <exception cref="OrkutException">OrkutException, when recieved through API</exception>
        /// <returns></returns>
        internal static bool PostActivity(string activityTitle, string activityBody)
        {
            var act = new JObject
                              {
                                  {"title", JToken.FromObject(activityTitle)},
                                  {"body", JToken.FromObject(activityBody)}
                              };
            var extraData = new Dictionary<string, string> 
            {
                { "method", "activities.create" },
                { "userId", "@me" },
                { "groupId", "@self" },
                {"activity", JsonConvert.SerializeObject(act)},
                {"appId", "@app"},
            };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            if (string.IsNullOrEmpty(requestResponse))
            {
                return true;
            }

            throw new OrkutException(requestResponse);
        }
    }
}
